from __future__ import division

import math

from sympy.abc import x


def choose_functions_number(method, eps):
    n = 1
    xs = [-1 + 1 / 10 * i for i in range(1, 20)]
    sym_y1 = method(n)
    while 1:
        n += 1
        sym_y2 = method(n)
        ys1 = [sym_y1.subs(x, xi) for xi in xs]
        ys2 = [sym_y2.subs(x, xi) for xi in xs]
        dy_vals = [math.fabs(y1 - y2) for y1, y2 in zip(ys1, ys2)]
        print 'testing {0} functions, max difference - {1}'.format(n, max(dy_vals))
        print
        dy_vals = [True if dy < eps else False for dy in dy_vals]
        if all(dy_vals):
            print '{0} functions is good enough for {1} method with eps={2}'.format(n, method.__name__, eps)
            print
            return n
        sym_y1 = sym_y2
